﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;

namespace Agent
{
#if !WindowsCE  
    static class Program
    {
        static ServiceAgent m_service = null;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            m_service = new ServiceAgent();
            ServicesToRun = new System.ServiceProcess.ServiceBase[] 
			{ 
				m_service 
			};

            if (Environment.GetCommandLineArgs().Contains<string>("-console"))
            {
                RunInConsoleMode();
            }
            else
            {
                System.ServiceProcess.ServiceBase.Run(ServicesToRun);
            }
        }

        static void RunInConsoleMode()
        {
            AllocConsole();

            Console.WriteLine("Running service in console mode, press a key to stop...");

            m_service.Run(Environment.GetCommandLineArgs());

            Console.ReadKey();

            Console.WriteLine("\nStopping...");

            m_service.Finish();

            Console.WriteLine("Service stopped.");
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool AllocConsole();

    }   
#else //!WindowsCE
    class Program
    {
        static void Main(string[] args)
        {
            Mimoza.AgentLayer.Configuration configuration = new Mimoza.AgentLayer.Configuration();

            m_application = new Mimoza.Apps.Agent.Application(configuration);
            m_application.Init();

            m_application.DoWork();
        }

        static Mimoza.Apps.Agent.Application m_application = null;
    }
#endif
}
